//
//  UserAccount.m
//  QiYReader
//
//  Created by 奇阅科技 on 14-11-15.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "UserAccount.h"
#import "PathHelper.h"
static UserAccount *account;
@implementation UserAccount
+ (NSString *)getAccountsStoragePath {
    return [[PathHelper documentDirectoryPathWithName:@"db"]
            stringByAppendingPathComponent:@"accounts"];
}

+ (void)loadAccount {
    account = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getAccountsStoragePath]];
}

+ (void)saveAccount:(UserAccount *)acc {
    [NSKeyedArchiver archiveRootObject:acc toFile:[self getAccountsStoragePath]];
    account = acc;
}
-(instancetype)init{
    self = [super init];
    if (self) {
    }
    return self;
}
+ (void)removeAccount {
    @synchronized(self) {
        if (account) {
            account = nil;
            [self saveAccount:account];
        }
    }
}

+ (UserAccount *)currentAccount {
    @synchronized(self) {
        if (!account) {
            [self loadAccount];
        }
    }
    return account;
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];
    if (self != nil) {
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [super encodeWithCoder:encoder];
}
+(UserAccount *)getFromDict:(NSDictionary *)dict{
    UserAccount *user = [UserAccount new];
    user.uid = [dict objectForKey:@"uid"];
    user.username = [dict objectForKey:@"username"];
    user.mobile = [dict objectForKey:@"mobile"];
    user.email = [dict objectForKey:@"email"];
    user.token = [dict objectForKey:@"token"];
    user.vip_level = [dict objectForKey:@"vip_level"];
    user.is_baoyue = [dict objectForKey:@"is_baoyue"];
    user.nickname = [dict objectForKey:@"nickname"];
    user.headimg = [dict objectForKey:@"headimg"];
    user.remain = [dict objectForKey:@"remain"];
    if (user.remain.length <1) {
        user.remain = @"0";
    }
    user.points = [dict objectForKey:@"points"];
    if (user.points.length<1) {
        user.points = @"0";
    }
    user.hadBuyNum = [dict objectForKey:@"hadBuyNum"];
    if (user.hadBuyNum.length<1) {
        user.hadBuyNum = @"--";
    }
    user.bookReviewNum = [dict objectForKey:@"bookReviewNum"];
    if (user.bookReviewNum.length<1) {
        user.bookReviewNum = @"--";
    }
    user.collectionNum = [dict objectForKey:@"collectionNum"];
    if (user.collectionNum.length<1) {
        user.collectionNum = @"--";
    }
    return user;
}
@end
